﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using OptusMobile.Web.ServerComponents.DAL;
using CommonServiceLibrary.EntityFramework;

namespace OptusMobile.Web.ServerComponents.Common
{
    /// <summary>
    /// The base class of any BAL class invoking Entity Framework DAL classes. This base class uses the AspNetObjectContextManager 
    /// class to shared one ObjectContext instance during the lifespan of the HTTP request.
    /// </summary>
    /// <typeparam name="T">An EntityObject type.</typeparam>
    public abstract class EFManagerBase
    {
        private AspNetObjectContextManager<RecommendationSysEntities> _objectContextManager;

        /// <summary>
        /// Returns the current ObjectContextManager instance. Encapsulated the 
        /// _objectContextManager field to show it as an association on the class diagram.
        /// </summary>
        private AspNetObjectContextManager<RecommendationSysEntities> ObjectContextManager
        {
            get { return _objectContextManager; }
            set { _objectContextManager = value; }
        }

        /// <summary>
        /// Returns a NorthwindObjectContext object. 
        /// </summary>
        protected internal RecommendationSysEntities ObjectContext
        {
            get
            {
                if (ObjectContextManager == null)
                {
                    this.ObjectContextManager = new AspNetObjectContextManager<RecommendationSysEntities>();
                }

                return ObjectContextManager.ObjectContext;
            }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public EFManagerBase()
        { }

        /// <summary>
        /// Persists all changes to the underlying datastore.
        /// </summary>
        public void SaveAllObjectChanges()
        {
            this.ObjectContext.SaveChanges();
        }


    }
}
